package org.jsoft.model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import org.jsoft.constants.Constants;
import org.jsoft.constants.ImageConstants;
import org.jsoft.panel.BgFrame;

public class Earth extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private BufferedImage[] pic = ImageConstants.earthArr;
	private int picIndex = 0;
	
	public void init(){
		this.setSize(pic[0].getWidth(), pic[0].getHeight());
//		this.setBorder(new LineBorder(Color.white));
	}

	protected void paintComponent(Graphics g) {
		g.drawImage(pic[picIndex % pic.length], 0, 0, null);
		picIndex++;
	}
	
	public static int num = 0;
	public static void move(){
		num++;
	}
	
	private double degree = Constants.EARTH_DEGREE;
	
	int startX=0;
	int startY=0;
	boolean start=true;
	int count=0;
	// x*x + y*y = r*r
	public void moving() {
		count++;
		int	solarCenterX = BgFrame.sun.getX() + BgFrame.sun.getWidth() / 2;
		int	solarCenterY = BgFrame.sun.getY() + BgFrame.sun.getHeight() / 2;
		
		if(num <= 0){
			return;
		}
//		int x = (int)(solarCenterX + 300 * Math.cos(degree));
//		int y = (int)(solarCenterY + 250 * Math.sin(degree));
		int x=-(int) (200 * Math.sin(Math.PI * degree / 180)) + solarCenterX;
		int y=(int) (200 * Math.cos(Math.PI * degree / 180)) + solarCenterY;
		if(start){
			startX=x;
			startY=y;
			start=false;
		}else{
			if(count>4){
				int a=startX-x;
				int b=startY-y;
				if(-5<= a && a<=5 && -5<= b && b<=5){
					num--;
				}
			}
		}
		setLocation(x, y);
		degree += Constants.step;
//		System.out.println(startX+","+startY+"--"+x+","+y+"--"+degree+"--count:"+count+"--num:"+num);
	}
}
